package com.gym.core.common;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @author xinchun.wang 
   @email: 532002108@qq.com
 * @createTime 2015-1-30 下午2:20:58
 */
public class CookieUtils {
	/**
	 * 获取cookie信息
	 * @param request web请求
	 * @param name cookie的名字
	 * @return
	 */
	public static String getCookie(HttpServletRequest request, String name) {
		Cookie[] cookies = request.getCookies();
		if (cookies == null || name == null) {
			return "";
		}
		
		for (Cookie c : cookies) {
			if (name.equals(c.getName())) {
				return c.getValue();
			}
		}
		
		return "";
	}
	
	
	/**
	 * 添加cookie
	 * @param cookieKey
	 * @param cookieValue
	 * @param response
	 */
	public static void addCookie(String cookieKey, String cookieValue, int maxAge,String domain,HttpServletResponse response) {
        Cookie cookie = new Cookie(cookieKey, cookieValue);
        cookie.setPath("/");
        cookie.setMaxAge(maxAge);
        cookie.setDomain(domain);
        if (response != null) {
            response.addCookie(cookie);
        }
    }
	
}
